package com.magic.smartcity.view.more.hospital

import android.os.Bundle
import android.text.style.BulletSpan
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import com.magic.smartcity.R
import com.magic.smartcity.beans.HospitalDepartments
import com.magic.smartcity.beans.HospitalPatientCard
import com.magic.smartcity.databinding.FragmentHospitalDepartmentsBinding
import com.magic.smartcity.view.more.viewmodels.HospitalViewModel

class HospitalDepartmentsFragment : Fragment() {

    private lateinit var binding: FragmentHospitalDepartmentsBinding
    private val list: MutableList<String> = mutableListOf()
    private lateinit var patientCardDetail: HospitalPatientCard
    private val viewModel by lazy {
        ViewModelProvider(this).get(HospitalViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        patientCardDetail = arguments?.getSerializable("patientCardDetail") as HospitalPatientCard
        viewModel.getDepartments()
    }


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentHospitalDepartmentsBinding.inflate(layoutInflater)
        initView()
        return binding.root
    }

    private fun initView() {
        val arrayAdapter =
            ArrayAdapter(requireContext(), R.layout.support_simple_spinner_dropdown_item, list)
        binding.departmentsList.adapter = arrayAdapter
        viewModel.departments.observe(viewLifecycleOwner) {
            list.clear()
            for (i in it) {
                list.add(i.categoryName)
            }
            arrayAdapter.notifyDataSetChanged()
            binding.departmentsList.setOnItemClickListener { parent, view, position, id ->
                val bundle = Bundle()
                bundle.putSerializable("departmentsDetail", it[position])
                bundle.putSerializable("patientCardDetail", patientCardDetail)
                //跳转至科室详情进行预约挂号
                Navigation.findNavController(view).navigate(
                    R.id.action_hospitalDepartmentsFragment_to_hospitalDepartmentsDetailFragment,
                    bundle
                )
            }
        }
    }
}